package com.jmb.api.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.common.exception.WebApiException;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.entity.InterfaceProcessor;
import com.jmb.api.vo.Interface.Pro.InterfaceProcessorVO;
import com.jmb.api.web.mapper.InterfaceCaseMapper;
import com.jmb.api.web.service.InterfaceProcessorService;
import com.jmb.api.web.mapper.InterfaceProcessorMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.NamingEnumeration;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 金梦博
* @description 针对表【t_interface_processor】的数据库操作Service实现
* @createDate 2025-09-25 17:19:37
*/
@Slf4j
@Service
public class InterfaceProcessorServiceImpl extends ServiceImpl<InterfaceProcessorMapper, InterfaceProcessor>
    implements InterfaceProcessorService{

    @Autowired
    private InterfaceProcessorMapper interfaceProcessorMapper;
    @Autowired
    private InterfaceCaseMapper interfaceCaseMapper;

    /**
     * 新增/更新接口处理器
     * @param interfaceProcessor 接口处理器
     * @return 添加/更新后的接口处理器
     */
    @Override
    public InterfaceProcessor saveOrUpdateInterfaceProcessor(InterfaceProcessor interfaceProcessor) {
        // 检查缺省值
        Integer haveDefaultValue = interfaceProcessor.getHaveDefaultValue();
        // 存在缺省值
        String defaultValue = interfaceProcessor.getDefaultValue();
        if (haveDefaultValue == 0){ //存在缺省值，阙兴志不允许为空
            if (defaultValue == null){
                throw new WebApiException(ResultCodeEnum.DEFAULT_VALUE_NOT_NULL_ERROR);
            }
        }else if (haveDefaultValue == 1){
            // 不存在缺省值，将defaultValue置空
            interfaceProcessor.setDefaultValue(null);
        }else {
            throw new WebApiException(ResultCodeEnum.HAVE_DEFAULT_VALUE_ERROR);
        }
        // 检查caseId是否合法
        Long caseId = interfaceProcessor.getCaseId();
        if (caseId == null){
            throw new WebApiException(ResultCodeEnum.PARAM_ERROR);
        }
        // 检查用例编号是否存在
        if (interfaceCaseMapper.selectInterfaceCaseByCaseId(caseId) ==  null){
            log.error("用例编号不存在，caseId={}",caseId);
            throw new WebApiException(ResultCodeEnum.CASE_NOT_EXIST);
        }
        // 通过Id判断是新增还是修改
        boolean isUpdate = interfaceProcessor.getId() != null;
        LambdaQueryWrapper<InterfaceProcessor> processorWrapper = new LambdaQueryWrapper<>();
        processorWrapper.eq(InterfaceProcessor::getName, interfaceProcessor.getName());
        // 如果是修改操作，我们先排除自身
        if (isUpdate){
            processorWrapper.ne(InterfaceProcessor::getId, interfaceProcessor.getId());
        }
        // 查询是否有相同的名称
        InterfaceProcessor processor = interfaceProcessorMapper.selectOne(processorWrapper);
        if (processor != null){
            log.error("名称已存在，name={}",interfaceProcessor.getName());
            throw new WebApiException(ResultCodeEnum.NAME_EXIST_ERROR);
        }
        super.saveOrUpdate(interfaceProcessor);

        return interfaceProcessor;
    }

    /**
     * 根据用例编号查询所有接口处理编号
     * @param CaseId 用例编号
     * @return 接口处理编号
     */
    @Override
    public List<Long> findInterfaceProcessorIdByCaseId(Long CaseId) {
        return interfaceProcessorMapper.selectInterfaceProcessorIdByCaseId(CaseId);
    }

    /**
     * 根据名称查询接口处理器
     * @param name 名称
     * @return 接口处理器
     */
    @Override
    public InterfaceProcessorVO findInterfaceProcessorByName(String name) {
        LambdaQueryWrapper<InterfaceProcessor> processorWrapper = new LambdaQueryWrapper<>();
        processorWrapper.eq(InterfaceProcessor::getName, name);
        InterfaceProcessor processor = interfaceProcessorMapper.selectOne(processorWrapper);
        InterfaceProcessorVO interfaceProcessorVO = new InterfaceProcessorVO();
        BeanUtils.copyProperties(processor,interfaceProcessorVO);
        return interfaceProcessorVO;
    }

    /**
     * 检查接口处理器名称
     * @param processorId 接口处理器编号
     * @param name 接口处理器名称
     * @return 存在相同名称的接口处理器
     */

    @Override
    public List<InterfaceProcessorVO> checkInterfaceProcessorName(Integer processorId, String name) {
        LambdaQueryWrapper<InterfaceProcessor> processorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        processorLambdaQueryWrapper.eq(InterfaceProcessor::getName, name)
                .ne(InterfaceProcessor::getId, processorId);
        List<InterfaceProcessor> processorList = interfaceProcessorMapper.selectList(processorLambdaQueryWrapper);
        return processorList.stream()
                .map(processor -> {
                    InterfaceProcessorVO interfaceProcessorVO = new InterfaceProcessorVO();
                    BeanUtils.copyProperties(processor, interfaceProcessorVO);
                    return interfaceProcessorVO;
                }).collect(Collectors.toList());
    }
}




